package hr.tvz.lab.tom.malwaredb.info;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.TextView;

import com.raizlabs.android.dbflow.sql.language.Select;

import java.util.ArrayList;
import java.util.List;

import hr.tvz.lab.tom.malwaredb.R;
import hr.tvz.lab.tom.malwaredb.add.AddActivity;
import hr.tvz.lab.tom.malwaredb.entity.MalwareObject;
import hr.tvz.lab.tom.malwaredb.entity.MalwareObject_Table;
import hr.tvz.lab.tom.malwaredb.list.RecyclerViewAdapter;

/**
 * Activity for general MalwareObject information.
 */
public class InfoActivity extends AppCompatActivity {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        // Back button
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        // Set title if object exists.
        MalwareObject malwareObject = getIntent().getExtras().getParcelable("malware");
        if (malwareObject != null) {
            getSupportActionBar().setTitle(malwareObject.getName());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_info, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // Back button.
                finish();
                return true;
            case R.id.menu_edit:
                // Send MalwareObject to edit, wait for result.
                Intent intent = new Intent(this, AddActivity.class);
                intent.putExtra("malware", getIntent().getExtras().getParcelable("malware"));
                startActivityForResult(intent, RESULT_FIRST_USER);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // If successful, go back. Updating content from here is too difficult.
        if (resultCode == RESULT_OK) {
            setResult(RESULT_OK);
            finish();
        }
    }

    public static class PlaceholderFragment extends Fragment {
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView;
            MalwareObject malwareObject = getActivity().getIntent().getExtras().getParcelable("malware");
            List<String> urlList = new ArrayList<>();
            switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
                // Info tab
                case 1:
                    // Elements
                    rootView = inflater.inflate(R.layout.fragment_item, container, false);
                    TextView type = (TextView) rootView.findViewById(R.id.textType);
                    TextView platform = (TextView) rootView.findViewById(R.id.textPlatform);
                    TextView sourceLanguage = (TextView) rootView.findViewById(R.id.textSourceLanguage);
                    TextView fileType = (TextView) rootView.findViewById(R.id.textFileType);
                    TextView size = (TextView) rootView.findViewById(R.id.textSize);
                    TextView author = (TextView) rootView.findViewById(R.id.textAuthor);
                    TextView origin = (TextView) rootView.findViewById(R.id.textOrigin);
                    TextView dateDiscovery = (TextView) rootView.findViewById(R.id.textDateDiscovery);
                    TextView aliases = (TextView) rootView.findViewById(R.id.textAliases);
                    TextView description = (TextView) rootView.findViewById(R.id.textDescription);
                    // Setting up values
                    if (malwareObject != null) {
                        type.setText(malwareObject.getType());
                        platform.setText(malwareObject.getPlatform());
                        sourceLanguage.setText(malwareObject.getSourceLanguage());
                        fileType.setText(malwareObject.getFileType());
                        // If size is lower or equal to 0, treat it as unknown.
                        if (malwareObject.getSize() > 0) {
                            size.setText(String.format("%d", malwareObject.getSize()));
                        } else {
                            size.setText(R.string.unknown);
                        }
                        author.setText(malwareObject.getAuthor());
                        origin.setText(malwareObject.getOrigin());
                        dateDiscovery.setText(malwareObject.getDateDiscovery());
                        // Get aliases in predefined format.
                        aliases.setText(malwareObject.getAliasesToString());
                        description.setText(malwareObject.getDescription());
                        // Receive only links on list. Media is not added.
                        urlList = malwareObject.getLinksListByType("link");
                    }
                    RecyclerView recyclerViewLinks = (RecyclerView) rootView.findViewById(R.id.recyclerViewLinks);
                    recyclerViewLinks.setHasFixedSize(true);
                    // Disable scrolling with alternate LinearLayoutManager class.
                    recyclerViewLinks.setLayoutManager(new NoScrollLinearLayoutManager(rootView.getContext()));
                    recyclerViewLinks.setAdapter(new RecyclerViewInfoLinksAdapter(urlList));
                    break;
                // Media tab
                case 2:
                    rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
                    if (malwareObject != null) {
                        // Receive only media on list. Links are not added here.
                        urlList = malwareObject.getLinksListByType("media");
                    }
                    RecyclerView recyclerViewImage = (RecyclerView) rootView.findViewById(R.id.recyclerViewGallery);
                    recyclerViewImage.setHasFixedSize(true);
                    recyclerViewImage.setLayoutManager(new LinearLayoutManager(rootView.getContext()));
                    recyclerViewImage.setAdapter(new RecyclerViewGalleryAdapter(urlList));
                    break;
                // Whatever it gets broken tab
                default:
                    rootView = inflater.inflate(R.layout.fragment_info, container, false);
                    TextView textView = (TextView) rootView.findViewById(R.id.section_label);
                    textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
                    break;
            }
            return rootView;
        }
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Info";
                case 1:
                    return "Media";
            }
            return null;
        }
    }
}
